package com.zhuoyue.biz.core.command.impl.intelligence;

import java.util.Map;

import com.zhuoyue.biz.core.command.model.SceneCmd;
import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zhuoyue.biz.core.command.Device;
import com.zhuoyue.biz.core.command.constant.AutomateCmdDeal;
import com.zhuoyue.common.utils.CmdUtils;

/**
 * Demo class
 * 设备自动化联动
 *
 * @author wjming
 * @date 2016/10/31
 */
public class Automate implements Device {

    @Override
    public String getCommand(String controlParam) {

        Map<String, String> map = (Map) JSONObject.parse(controlParam);

        String controlType = map.get("controlType");

        String cmd = "";

        if (AutomateCmdDeal.AutomateDealType.AUTOMATE_CONFIGURE.equals(controlType)) {
            //配置
            cmd = getAutomateConfigureCmd(map);
        } else if (AutomateCmdDeal.AutomateDealType.AUTOMATE_LOAD.equals(controlType)) {
            //载入
            cmd = getAutomateLoadCmd(map);
        } else if (AutomateCmdDeal.AutomateDealType.AUTOMATE_CLEAR.equals(controlType)) {
            //删除
            cmd = getAutomateClearCmd(map);
        }

        return cmd;

    }

    /**
     * 删除自动化
     *
     * @param map
     * @return
     */
    private String getAutomateClearCmd(Map<String, String> map) {
        String gatewayMac = map.get("gatewayMac");
        String automateNumber = map.get("automateNumber");
        automateNumber = CmdUtils.intToHexTwo(Integer.parseInt(automateNumber));
        return new SceneCmd(gatewayMac, automateNumber).getCmd(Scene.CMD_MODE_CLEAR);

    }


    /**
     * 自动化配置
     *
     * @param map
     * @return
     */
    private String getAutomateConfigureCmd(Map<String, String> map) {

        String gatewayMac = map.get("gatewayMac");
        String deviceMac = map.get("deviceMac");
        String identity = map.get("identity");
        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));
        String automateNumber = actionsMap.get("automateNumber");
        String cmdCount = actionsMap.get("cmdCount");
        String controlState = actionsMap.get("controlState");
        String defendStatus = actionsMap.get("defendStatus");
        String temperature = actionsMap.get("temperature");


        //自动化数字
        automateNumber = CmdUtils.intToHexTwo(Integer.parseInt(automateNumber));

        cmdCount = CmdUtils.intToHexTwo(Integer.parseInt(cmdCount));
        String cmd22 = null;
        String dataType = "01";

        if (StringUtils.isNotEmpty(identity)) {
            //打开命令
            String countStateNumberStart = "1";
            //结束命令
            String countStateNumberEnd = "2";
            //验证是否是新版本的温湿度传感器
            String identityCode = "0FBEA002";

            if (identityCode.equals(identity)) {
                if (countStateNumberStart.equals(controlState)) {
                    //打开命令对应温度上限
                    controlState = "03";
                }
                if (countStateNumberEnd.equals(controlState)) {
                    //关闭命令对应下限
                    controlState = "04";
                }
                dataType = "02";
                String temperatureRemovePoint = null;
                if (StringUtils.isNotEmpty(temperature)) {
                    String point = ".";
                    if (StringUtils.indexOf(temperature, point) > 0) {
                        //去掉"."
                        temperatureRemovePoint = StringUtils.remove(temperature, point);

                        cmd22 = "00000" + temperatureRemovePoint;
                    }
                    if (StringUtils.indexOf(temperature, point) < 1) {
                        cmd22 = "0000" + temperatureRemovePoint + "00";
                    }
                }
            } else {
                cmd22 = "000000" + CmdUtils.intToHexTwo(Integer.parseInt(controlState));
                controlState = "01";
            }

        } else {
            cmd22 = "000000" + CmdUtils.intToHexTwo(Integer.parseInt(controlState));
            controlState = "01";
        }

        //设置自动化巡查时间
        String triggerTime = actionsMap.get("triggerTime");
        if (StringUtils.isEmpty(triggerTime)) {
            triggerTime = AutomateCmdDeal.AutomateConfigure.CMD_21;
        }
        String status = AutomateCmdDeal.AutomateConfigure.CMD_16;
        if (StringUtils.isNotEmpty(defendStatus)) {
            status = defendStatus;
        }

        String cmd7To24 = "01000000000000000000000000000000000000000002" + automateNumber + "00" + cmdCount + "FFFFFF" + "0201" + status + deviceMac + "01" +
                dataType + controlState + triggerTime + cmd22 + "FFFFFFFF0000";

        String cmdDataLength = CmdUtils.getDateLength(cmd7To24);

        String cmd3To24 = "5000" + gatewayMac + cmdDataLength + cmd7To24;

        //数据长度
        String cmd2 = CmdUtils.getDateLength(cmd3To24);

        String cmd25 = CmdUtils.getCmdBodySum(cmd3To24);

        //帧头2A
        return "2A" + cmd2 + cmd3To24 + cmd25 + "23";
    }


    private String getAutomateLoadCmd(Map<String, String> map) {

        String gatewayMac = map.get("gatewayMac");
        Map<String, String> actionsMap = (Map) JSONObject.parse(JSON.toJSONString(map.get("actions")));
        String automateNumber = actionsMap.get("automateNumber");
        String cmdCount = actionsMap.get("cmdCount");
        String orderNumber = actionsMap.get("orderNumber");
        String loadCmd = actionsMap.get("loadCmd");
        String timeExpand = actionsMap.get("timeExpand");

        String cmd8 = CmdUtils.intToHexTwo(Integer.parseInt(automateNumber));
        String cmd9 = CmdUtils.intToHexTwo(Integer.parseInt(cmdCount));
        String cmdTimeExpand = CmdUtils.intToHexTwo(Integer.parseInt(timeExpand));

        String cmd10 = CmdUtils.intToHexTwo(Integer.parseInt(orderNumber));

        String cmd7To11 = AutomateCmdDeal.AutomateLoad.CMD_7 + cmd8 + cmd9 + cmd10 + loadCmd + cmdTimeExpand;

        String cmd6 = CmdUtils.getDateLength(cmd7To11);

        String cmd3To11 = AutomateCmdDeal.AutomateLoad.CMD_3 + AutomateCmdDeal.AutomateLoad.CMD_4 + gatewayMac +
                cmd6 + cmd7To11;

        String cmd2 = CmdUtils.getDateLength(cmd3To11);

        String cmd12 = CmdUtils.getCmdBodySum(cmd3To11);

        return AutomateCmdDeal.AutomateLoad.CMD_1 + cmd2 + cmd3To11 + cmd12 + AutomateCmdDeal.AutomateLoad.CMD_13;
    }


}
